QX=...
    [[0,168,225];[153,204,0];[227,0,57];
[252,211,0];[128,0,128];[0,153,78];
[255,102,0];[128,128,0];[219,0,194];
[0,128,128];[0,0,255];[200,204,0]]/255;
%%
diretory = "F:\Documents\CST\self_design_1\里程\n12\导出文本";
var = 'n';RANGE=1:0.1:3;
% var = 'nold';RANGE=1:0.1:3;
peaksdetail = [];
%%
for each =  RANGE% txt的命名要按这个来
    myfilename = diretory+['\',num2str(each),'.txt'];%文件名
    tmp=readsweep(myfilename,each);
%     savetotal=[savetotal;tmp];
%{1
    %如果要显示每个变量名就打开这段程序
    if each>floor(each) %如果文件中有小数，把小数中的.换成_
        xiaoshu = (each - floor(each)+0.001)*10;
        % 这里+.01是因为floor有bug，有时floor(2)=1
%         floor(xiaoshu)
%         num2str(floor(xiaoshu))
        makename=[var,'_',...
            num2str(floor(each)),...
            '_',num2str(floor(xiaoshu))];% 先定义这样一个变量名
    else
        makename=[var,'_',num2str(each)];% 先定义这样一个变量名
    end
    eval([makename,'=','tmp',';']); 
    C= PEAK(tmp);
    peaksdetail = [peaksdetail;PEAK(tmp)];
%}
    clear tmp;
end

%% f0

% [~,f0] = findpeaks(nold_1(:,2),nold_1(:,1));
[~,f0] = findpeaks(eval([var,'_1','(:,2)']),eval([var,'_1','(:,1)']));

%% FRE SHIFT; since it is double-peak, we definite 2 fs values
%% WD: peak's width
[NUM,~] = size(peaksdetail);NUM=NUM/6; 
FS1 = [];FS2 = [];
WD1 = [];WD2 = [];
for every = 1:NUM
    FS1 = [FS1;peaksdetail(6*every-4)-f0(1)];
    FS2 = [FS2;peaksdetail(6*every-1)-f0(2)];
    WD1 = [WD1;peaksdetail(6*every-3)];
    WD2 = [WD2;peaksdetail(6*every)];
end
YLABEL='FS(THz)';
%% 另外一种FOM
FS1 = FS1./WD1;
FS2 = FS2./WD2;YLABEL='FS/B';
% FS1 = FS1./f0(1)*100;
% FS2 = FS2./f0(2)*100;YLABEL='FS(%)'
%% 拟合
RANGE=1:0.1:3;
P1 = polyfit(RANGE,FS1,1);
FSNEW1 = polyval(P1,RANGE);
P2 = polyfit(RANGE,FS2,1);
FSNEW2 = polyval(P2,RANGE);
%% 如果要同时绘两个图，在这里暂停之后，保住前面几个值，再重新换文件运行前面的程序
FSA = FS1;FSB = FS2;
FSNEWA = FSNEW1; FSNEWB = FSNEW2;
clearvars -except FSA FSB FSNEWA FSNEWB;
%% 绘图
%{1
general = {'figure name', ['折射率的变化'];
    'title name',' ';
    'x_label','Refractive Index';
    'y_label',YLABEL;
    };
close all;
data = {'x', 'y','legend','color','LineStyle';
    RANGE,FS1,'f_1',QX(1,:),'s';
    RANGE,FSNEW1,'f_1 fit',QX(1,:),'-';
    RANGE,FS2,'f_2',QX(11,:),'^';
    RANGE,FSNEW2,'f_2 fit',QX(11,:),'-.';
    };
other = {
    'y_label2',YLABEL;
    'second y',1;
    'second legend', 0;
    };
%%
another_axis = {'x', 'y','lengend','color','LineStyle';
    RANGE,FSA,'f_1',QX(3,:),'s';
    RANGE,FSNEWA,'f_1 fit',QX(3,:),'-';
    RANGE,FSB,'f_2',QX(7,:),'^';
    RANGE,FSNEWB,'f_2 fit',QX(7,:),'-.';
    };
hand = YW_Plot1(general,data,other,another_axis);
% set(gca(hand),'XLim',[.75 1.9]);
% set(gca(hand),'XTick',[.75 1.9])
%}
